/**
 * 
 */
package com.oet.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;




/**
 *<book>
 *	<name><![CDATA[value]]</name>
 *	<coverLarg>cover_large.jpg</coverLarg>
 *	<coverSmal>cover_small.jpg</coverSmal>
 *	<zipFile><![CDATA[value]</zipFile>
 *	<resourceSet><![CDATA[value]</resourceSet>
 *	<grade><![CDATA[value]</grade>
 *	<provider><![CDATA[value]</provider>
 *	<provider><![CDATA[value]</provider>
 *	<provider><![CDATA[value]</provider>
 *	<provider><![CDATA[value]</provider>
 *	<provider><![CDATA[value]</provider>
 *	<provider><![CDATA[value]</provider>
 *	<provider><![CDATA[value]</provider>
 *	<provider><![CDATA[value]</provider>
 *</book>
 */
public class TemplateXmlParser {
	
	private static TemplateXmlParser bookXml = null;
	private static Logger log4j = Logger.getLogger(TemplateXmlParser.class);
	
	public final static String ELEMENT_NAME_ROOT = "book";
	public final static String ELEMENT_NAME_BOOK_NAME = "name";
	public final static String ELEMENT_NAME_BOOK_YDKBOOKID = "ydkBookId";
	public final static String ELEMENT_NAME_BOOK_COVERLARGE = "coverLarge";
	public final static String ELEMENT_NAME_BOOK_COVERSMALL = "coverSmall";
	public final static String ELEMENT_NAME_BOOK_ZIPFILE = "zipFile";
	public final static String ELEMENT_NAME_BOOK_RESOURCESET = "resourceSet";
	public final static String ELEMENT_NAME_BOOK_GRADE = "grade";
	public final static String ELEMENT_NAME_BOOK_PROVIDER = "provider";
	public final static String ELEMENT_NAME_BOOK_AUTHOR = "author";
	public final static String ELEMENT_NAME_BOOK_ISBN = "isbn";
	public final static String ELEMENT_NAME_BOOK_KEYWORD = "keyword";
	public final static String ELEMENT_NAME_BOOK_PUBLISHER = "publisher";
	public final static String ELEMENT_NAME_BOOK_PUBLISHTIME = "publishTime";
	public final static String ELEMENT_NAME_BOOK_INTRO = "intro";
	public final static String ELEMENT_NAME_BOOK_CATEGORY = "category";
	public final static String ELEMENT_NAME_BOOK_INDEX = "index";
	
	public final static String ELEMENT_NAME_BOOK_CHAPTER = "chapter";
	public final static String ELEMENT_NAME_BOOK_CHAPTER_ID = "id";
	public final static String ELEMENT_NAME_BOOK_CHAPTER_NAME = "name";
	public final static String ELEMENT_NAME_BOOK_CHAPTER_FILE = "file";
	public final static String ELEMENT_NAME_BOOK_CHAPTER_INTRODUCTION = "introduction";
	public final static String ELEMENT_NAME_BOOK_CHAPTER_ISFREE = "isFree";
	
	
	//Some Value defined by CM
	public final static String ELEMENT_VALUE_BOOK_COVERLARGE = "cover_large.jpg";
	public final static String ELEMENT_VALUE_BOOK_COVERSMALL = "cover_small.jpg";
	public final static String ELEMENT_VALUE_BOOK_ZIPFILE = "book.zip";
	public final static String ELEMENT_VALUE_BOOK_RESOURCESET = "7025,7026";
	public final static String ELEMENT_VALUE_BOOK_GRADE = "3";
	public final static String ELEMENT_VALUE_BOOK_PROVIDER = "2";
		
	private TemplateXmlParser(){
		super();
	}
	
	public static TemplateXmlParser getYdkXml() {
		if (bookXml == null) {
			bookXml = new TemplateXmlParser();
		}
		return bookXml;
	}
	
	/**
	 * 
	 * @param book
	 * @return
	 
	public Document createXmlFromYdkBook(YdkWebBook book)
	{
		Document doc = YdkXml.getYdkXml().createNewDocument();
		
		//Change null to ""
		
		if (doc != null)
		{
			//change the book to xml;
			Element root = doc.createElement(ELEMENT_NAME_ROOT);
			doc.appendChild(root);
			
			//name
			Element bookName = doc.createElement(ELEMENT_NAME_BOOK_NAME);
			root.appendChild(bookName);
			CDATASection bookNameValue = doc.createCDATASection(book.getTitle());
			bookName.appendChild(bookNameValue);
			
			//coverLarge
			Element coverLarge = doc.createElement(ELEMENT_NAME_BOOK_COVERLARGE);
			root.appendChild(coverLarge);
			coverLarge.appendChild(doc.createTextNode(ELEMENT_VALUE_BOOK_COVERLARGE));
			
			//coverSmall
			Element coverSmall = doc.createElement(ELEMENT_NAME_BOOK_COVERSMALL);
			root.appendChild(coverSmall);
			coverSmall.appendChild(doc.createTextNode(ELEMENT_VALUE_BOOK_COVERSMALL));
			
			//zipFile
			Element zipFile = doc.createElement(ELEMENT_NAME_BOOK_ZIPFILE);
			root.appendChild(zipFile);
			zipFile.appendChild(doc.createTextNode(ELEMENT_VALUE_BOOK_ZIPFILE));
			
			//resourceSet
			Element resourceSet = doc.createElement(ELEMENT_NAME_BOOK_RESOURCESET);
			root.appendChild(resourceSet);
			resourceSet.appendChild(doc.createTextNode(ELEMENT_VALUE_BOOK_RESOURCESET));
			
			//grade
			Element grade = doc.createElement(ELEMENT_NAME_BOOK_GRADE);
			root.appendChild(grade);
			grade.appendChild(doc.createTextNode(ELEMENT_VALUE_BOOK_GRADE));
			
			//provider
			Element provider = doc.createElement(ELEMENT_NAME_BOOK_PROVIDER);
			root.appendChild(provider);
			provider.appendChild(doc.createTextNode(ELEMENT_VALUE_BOOK_PROVIDER));
			
			//author
			Element author = doc.createElement(ELEMENT_NAME_BOOK_AUTHOR);
			root.appendChild(author);
			author.appendChild(doc.createCDATASection(book.getAuthor()));
			
			//isbn
			Element isbn = doc.createElement(ELEMENT_NAME_BOOK_ISBN);
			root.appendChild(isbn);
			isbn.appendChild(doc.createTextNode(book.getIsbn()));
			
			//keyword
			Element keyword = doc.createElement(ELEMENT_NAME_BOOK_KEYWORD);
			root.appendChild(keyword);
			
			
			//Here we need to split the situation: //For standard category and Unstandard Category.
			
			//For standard category
			String bookKeyWord = null;
			
			
//			if (book.getKeywords() != null && !book.getKeywords().isEmpty())
//			{
//				//we got the key words.
//				keyword.appendChild(doc.createCDATASection(book.getTilte() + ";" 
//						+ book.getAuthor() + ";" + book.getCategory()+ ";" + book.getKeywords()));
//			}
//			else
//			{
//				
//				Integer cateId = YdkBookUtility.convertToCmCategory(book.getCategory());
//				if (cateId != null)
//				{
//					bookKeyWord = YdkBookUtility.getKeywordByCmCateId(cateId);
//					
//					keyword.appendChild(doc.createCDATASection(book.getTilte() + ";" 
//							+ book.getAuthor() + ";" + book.getCategory()+ ";" + bookKeyWord));
//				}
//				else
//				{
//					ArrayList<String> keywordList = new ArrayList<String>(0);
//					if (book.getCategory() != null && !book.getCategory().isEmpty())
//					{
//						String[] keywordArray = null;
//						if (book.getCategory().contains(" "))
//						{
//							keywordArray = book.getCategory().split(" ");
//						}
//						else if (book.getCategory().contains("��"))
//						{
//							keywordArray = book.getCategory().split("��");
//						}
//						else if (book.getCategory().contains(">"))
//						{
//							keywordArray = book.getCategory().split(">");
//						}
//						else if (book.getCategory().contains("��"))
//						{
//							keywordArray = book.getCategory().split("��");
//						}
//						else if (book.getCategory().contains("��"))
//						{
//							keywordArray = book.getCategory().split("��");
//						}
//						else if (book.getCategory().contains("<"))
//						{
//							keywordArray = book.getCategory().split("<");
//						}
//						else if (book.getCategory().contains("��"))
//						{
//							keywordArray = book.getCategory().split("��");
//						}
//						else if (book.getCategory().contains("��"))
//						{
//							keywordArray = book.getCategory().split("��");
//						}
//						else if (book.getCategory().contains(";"))
//						{
//							keywordArray = book.getCategory().split(";");
//						}
//						else if (book.getCategory().contains("��"))
//						{
//							keywordArray = book.getCategory().split("��");
//						}
//						else if (book.getCategory().contains(","))
//						{
//							keywordArray = book.getCategory().split(",");
//						}
//						else if (book.getCategory().contains("��"))
//						{
//							keywordArray = book.getCategory().split("��");
//						}
//						else{
//							// Sorry, not supported 
//						}
//						
//						if (keywordArray != null)
//						{
//							// we've found something.
//							for (int i = 0; i < keywordArray.length; i++)
//							{
//								String tempKeyWord = keywordArray[i];
//								if (tempKeyWord != null)
//								{
//									tempKeyWord = tempKeyWord.replace(">", "");
//									tempKeyWord = tempKeyWord.replace("<", "");
//									tempKeyWord = tempKeyWord.replace("��", "");
//									tempKeyWord = tempKeyWord.replace("��", "");
//									tempKeyWord = tempKeyWord.replace("��", "");
//									tempKeyWord = tempKeyWord.replace("��", "");
//									tempKeyWord = tempKeyWord.replace(";", "");
//									tempKeyWord = tempKeyWord.replace("��", "");
//									tempKeyWord = tempKeyWord.replace(",", "");
//									tempKeyWord = tempKeyWord.replace("��", "");
//									tempKeyWord = tempKeyWord.trim();
//									if (!tempKeyWord.isEmpty())
//									{
//										keywordList.add(tempKeyWord);
//									}
//								}
//							}
//						}
//						else
//						{
//							//see it as a whole word.
//							keywordList.add(book.getCategory());
//						}
//						
//						
//						
//					}
//					else
//					{
//						//book.getCategory == null;
//						keywordList.add("����");
//						keywordList.add("����");
//					}
//					if (keywordList.size() <= 1)
//					{
//						keyword.appendChild(doc.createCDATASection(book.getTilte() + ";" 
//								+ book.getAuthor() + ";" + book.getCategory()+ ";" + "����"));
//					}
//					else{
//						keyword.appendChild(doc.createCDATASection(book.getTilte() + ";" 
//								+ book.getAuthor() + ";" + keywordList.get(0)+ ";" +  keywordList.get(1)));
//					}
//				}
//			}
			
			
			//For unstandard category

			//publisher
			Element publisher = doc.createElement(ELEMENT_NAME_BOOK_PUBLISHER);
			root.appendChild(publisher);
			publisher.appendChild(doc.createCDATASection(book.getPublisher()));
			
			//publishTime
			Element publishTime = doc.createElement(ELEMENT_NAME_BOOK_PUBLISHTIME);
			root.appendChild(publishTime);
			publishTime.appendChild(doc.createTextNode(book.getPublishDate().toLocaleString()));
			
			//intro
			Element intro = doc.createElement(ELEMENT_NAME_BOOK_INTRO);
			root.appendChild(intro);
			intro.appendChild(doc.createCDATASection(book.getSummary()));
			
			return doc;
		}
		
		return null;
	}
	*/
	
	
	


}
